<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;
class CartController extends Controller
{
    public function delete($id)
    {
       $res = DB::table('cart') -> where('id', $id) -> delete();
       if($res)
       {
             return redirect('/home/cart/index') -> with(['info' => '删除失败']);
             // return view('home.cart.index',['title' => '购物车']);
       }else
       {
            return redirect('/home/cart/index') -> with(['info' => '删除失败']);
       }
    }

    //
    public function index(Request $request)
    {   

       if(!session('master'))
       {
            $cart = $request->session()->all();
            $data = $cart['cart'];
            // dd($data);
            //数组
            // $data =  $request->session()->push('cart.cart', 'cart');
            
            // dd($data);
                $sum = 0;
                foreach ($data as $key => $value) {
                   $sum += $value['price'] * $value['scnt'];
                }
                Session::put('sum',$sum);
            return view('home.cart.index',['title' => '购物车','data' => $data]);
       }else
       {   
        if(session('cart') && session('cart_flag'))
            {
                Session::put('cart_flag',false);
                 $cart = $request->session()->all();
                 $data_0 = $cart['cart'];
                // dd($data_0);

                 $arr = $request->session()-> all();
                 $uid = $arr['master'] -> id;
                 foreach($data_0 as $k => $v)
                 {
                    $v['uid'] = $uid;
                    $gid = $v['gid'];
                    $data_goods = DB::table('goods') -> where('gid',$gid) -> first();
                    $data[$gid]['gid'] = $gid;
            $data[$gid]['uid'] = $uid;
            
            $data[$gid]['scnt'] = 1;
            $data[$gid]['total'] = $data_goods -> price * $data[$gid]['scnt'];
            
            $time = date('Y-m-d H:i:s');
       
            $data[$gid]['created_at'] = $time;
            $data[$gid]['updated_at'] = $time;
            unset($data[$gid]['goodspic']);
            unset($data[$gid]['goodsname']);
            unset($data[$gid]['price']);
            $res = DB::table('cart') -> insert($data[$gid]);
            $data[$gid]['goodspic'] =  $data_goods -> goodspic;
            $data[$gid]['goodsname'] =  $data_goods -> goodsname;
            $data[$gid]['price'] =  $data_goods -> price;
            // dd($data[$gid]);
            // Session::push('cart',$data[$gid]);
            Session::forget('cart');
            // dd(session('cart'));
            if(is_object($data[$gid])) {  
                $data[$gid] = (array)$data[$gid];  
             }
            // $data[$gid]= json_decode($data[$gid]);
            return redirect('/home/cart/index') -> with(['info' => '添加成功','data' => $data[$gid]]);

                 }

            }else{
               
            $uid = session('master') -> id;
            $data = DB::table('cart') 
                -> select('cart.*','goods.goodspic','goods.price','goodsname')
                -> leftJoin('goods','cart.gid','=','goods.gid')
                -> where('uid','=',$uid)
                -> get();
            // dd($data);
             //     if(is_object($data)) {  
             //    $data = (array)$data;  
             // }
                $data= json_decode($data);
             // dd($data);
                  $sum = 0;
                  foreach ($data as $key => $value) {
                        if(is_object($value)) {  
                        $value = (array)$value;  
                        $data[$key] = $value;
                 }
              
                    // dd($value);
                   $sum += $value['scnt'] * $value['price'];
                }
                Session::put('sum',$sum);
                // dd($sum);
            return view('home.cart.index',['title' => '购物车','data' => $data]); 
            }
       }
    }
    
    public function cut(Request $request)
    {   
        $id = $request -> id;
        $scnt = $request -> scnt;
        
        if($scnt >= 2)
        {

            $scnt = $scnt - 1;
        }

        $res = DB::table('cart') -> where("id", $id) -> update(["scnt" => $scnt]);
        
         $uid = session('master') -> id;
            $data = DB::table('cart') 
                -> select('cart.*','goods.goodspic','goods.price','goodsname')
                -> leftJoin('goods','cart.gid','=','goods.gid')
                -> where('uid','=',$uid)
                -> get();
            // dd($data);
            //     if(is_object($data)) {  
            //    $data = (array)$data;  
            // }
                $data= json_decode($data);
            // dd($data);
                  $sum = 0;
                  foreach ($data as $key => $value) {
                        if(is_object($value)) {  
                        $value = (array)$value;  
                        $data[$key] = $value;
                }
              
                    // dd($value);
                   $sum += $value['scnt'] * $value['price'];
                }
                Session::put('sum',$sum);

        if($res)
        {
            return $sum;
        }else
        {
            return 0;
        }
    }
    public function add(Request $request)
    {   


        $id = $request -> id;
        $scnt = $request -> scnt;
        
        $scnt_new= $scnt +1;
        $res = DB::table('cart') -> where("id", $id) -> update(["scnt" => $scnt_new]);
        $uid = session('master') -> id;
            $data = DB::table('cart') 
                -> select('cart.*','goods.goodspic','goods.price','goodsname')
                -> leftJoin('goods','cart.gid','=','goods.gid')
                -> where('uid','=',$uid)
                -> get();
            // dd($data);
            //     if(is_object($data)) {  
            //    $data = (array)$data;  
            // }
                $data= json_decode($data);
            // dd($data);
                  $sum = 0;
                  foreach ($data as $key => $value) {
                        if(is_object($value)) {  
                        $value = (array)$value;  
                        $data[$key] = $value;
                }
              
                    // dd($value);
                   $sum += $value['scnt'] * $value['price'];
                }
                Session::put('sum',$sum);

        if($res)
        {
            return $sum;
        }else
        {
            return $scnt;
        }
    }


    public function insert($gid,$uid=0)
    {   
        if($uid)
        {   
            $res = DB::table('cart') -> where('gid',$gid) -> where('uid',$uid) -> first();
            if($res)
            {   
                $scnt = $res -> scnt;
                $scnt += 1;
                $id = $res -> id;
                // dd($scnt);
                $arr = DB::table('cart') -> where('id',$id) -> update(['scnt' => $scnt]);
                 if($arr)
                {
                    // dd($arr);
                    return redirect('/home/cart/index') -> with(['info' => '添加成功']);
                }else
                {
                    return back() -> with(['info' => '添加失败']);
                }

            }
            // $uid = session('master') -> username; 
            $data_goods = DB::table('goods') -> where('gid',$gid) -> first();
            // $data_user = DB::table('users') -> where('id',$uid) -> first();
            // dd($data_user);
            $data[$gid]['gid'] = $gid;
            $data[$gid]['uid'] = $uid;
            
            $data[$gid]['scnt'] = 1;
            $data[$gid]['total'] = $data_goods -> price * $data[$gid]['scnt'];
            
            $time = date('Y-m-d H:i:s');
       
            $data[$gid]['created_at'] = $time;
            $data[$gid]['updated_at'] = $time;
            
            $res = DB::table('cart') -> insert($data[$gid]);
            $data[$gid]['goodspic'] =  $data_goods -> goodspic;
            $data[$gid]['goodsname'] =  $data_goods -> goodsname;
            $data[$gid]['price'] =  $data_goods -> price;
            // dd($data['gid']);
            Session::push('cart',$data[$gid]);

            if($res)
                {
                    return redirect('/home/cart/index') -> with(['info' => '添加成功','data' => $data[$gid]]);
                }else
                {
                    return back() -> with(['info' => '添加失败']);
                }
           
        }else
        {
            $data_goods = DB::table('goods') -> where('gid',$gid) -> first();
            // $data_user = DB::table('users') -> where('id',$uid) -> first();
            // dd($data_user);
            $data[$gid]['gid'] = $gid;
            $data[$gid]['uid'] = $uid;
            $data[$gid]['goodspic'] =  $data_goods -> goodspic;
            $data[$gid]['price'] =  $data_goods -> price;
            $data[$gid]['goodsname'] =  $data_goods -> goodsname;
            $data[$gid]['scnt'] = 1;
            $data[$gid]['total'] = $data_goods -> price * $data[$gid]['scnt'];;
            
            $time = date('Y-m-d H:i:s');
      
            $data[$gid]['created_at'] = $time;
            $data[$gid]['updated_at'] = $time;
            // dd($data);
            // if(!session('cart'))
            // {
            //      Session::put('cart',$data['gid']);
            // }
            
            Session::push('cart',$data[$gid]);
            if(session('cart'))
            {
                 return redirect('/home/cart/index') -> with(['info' => '添加成功','data' => $data[$gid]]);
            }else
            {
                return back() -> with(['info' => '添加失败']);
            }

        }
    }
}
